package com.example.demo.business.service;

import com.example.demo.base.bean.Meta;
import com.example.demo.base.exceptions.BaseException;
import com.example.demo.base.util.CommonUtil;
import com.example.demo.business.bean.entity.HAd;
import com.example.demo.business.bean.entity.HAdVert;
import com.example.demo.business.bean.entity.HAppment;
import com.example.demo.business.bean.entity.HUser;
import com.example.demo.business.bean.enums.AdType;
import com.example.demo.business.bean.enums.AppMentType;
import com.example.demo.business.bean.vo.AdItemVo;
import com.example.demo.business.bean.vo.HAppmentVo;
import com.example.demo.business.bean.vo.VideoItemVo;
import com.example.demo.business.dao.HAdDao;
import com.example.demo.business.dao.HAdVertDao;
import com.example.demo.business.dao.HAppmentDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdService {
    @Autowired
    private HAppmentDao hAppmentDao;

    @Autowired
    private HAdDao hAdDao;

    @Autowired
    private TokenService tokenService;

    //获取轮播图
    public List<AdItemVo> getBanners(){
        //获取没有禁用的轮播图
        List<HAd> adList = getAd(AdType.Banner);
        return adList.stream().map(item->{
            AdItemVo adItemVo = new AdItemVo();
            BeanUtils.copyProperties(item,adItemVo);
            return adItemVo;
        }).collect(Collectors.toList());
    }

    //获取闪屏广告
    public AdItemVo getSplashAd(){
        //
        List<HAd> adList = getAd(AdType.Splash);
        int length = adList.size();
        if (length== 0){
            throw new BaseException(Meta.SplashAdErr);
        }
        //获取随机值
        int num = CommonUtil.getRandomCount(length);
        HAd hAd = adList.get(num);
        AdItemVo adItemVo = new AdItemVo();
        BeanUtils.copyProperties(hAd,adItemVo);
        return adItemVo;
    }

    //随机获取条目中广告
    public VideoItemVo getItemAd(int type){
        //
        List<HAd> adList = getAd(AdType.Item);
        int length = adList.size();
        if (length== 0){
            return null;
        }
        //获取随机值
        int num = CommonUtil.getRandomCount(length);
        HAd hAd = adList.get(num);
        return new VideoItemVo(String.valueOf(0),hAd.getTitle(),"3小时前",hAd.getBannerUrl(),
                hAd.getBannerUrl(), hAd.getBannerUrl(),true,
                hAd.getLinks(),0f,"",false,false,"","",type);
    }

    //随机获取条目中广告
    public AdItemVo getDetailAd(int type){
        //
        List<HAd> adList = getAd(AdType.Detail);
        int length = adList.size();
        if (length== 0){
            return null;
        }
        //获取随机值
        int num = CommonUtil.getRandomCount(length);
        HAd hAd = adList.get(num);
        AdItemVo adItemVo = new AdItemVo();
        BeanUtils.copyProperties(hAd,adItemVo);
        return adItemVo;
    }

    //获取闪屏公告
    public HAppmentVo getSplashAppMent(){
        List<HAppment> list = getAppMent(AppMentType.Splash);
        int length = list.size();
        if (length== 0){
            return null;
        }
        int num = CommonUtil.getRandomCount(length);
        HAppment hAppment = list.get(num);
        HAppmentVo hAppmentVo = new HAppmentVo();
        BeanUtils.copyProperties(hAppment,hAppmentVo);
        //TODO 时间判断
        return hAppmentVo;
    }

    //获取专列公告
    public List<HAppmentVo> getSpecialAppMent(){
        List<HAppment> list = getAppMent(AppMentType.Special);
        int length = list.size();
        if (length== 0){
            return null;
        }
        return list.stream().map(item->{
            HAppmentVo hAppmentVo = new HAppmentVo();
            BeanUtils.copyProperties(item,hAppmentVo);
            return hAppmentVo;
        }).collect(Collectors.toList());
    }

    @Autowired
    private HAdVertDao hAdVertDao;
    //获取专列公告
    public List<HAdVert> getSpecialAd(){
        List<HAdVert> list = (List<HAdVert>) hAdVertDao.findAll();
        int length = list.size();
        if (length== 0){
            return null;
        }
        return list;
    }

    //获取首页公告
    public List<HAppmentVo> getBannerAppMent(){
        List<HAppment> list = getAppMent(AppMentType.Banner);
        int length = list.size();
        if (length== 0){
            return null;
        }
        return list.stream().map(item->{
            HAppmentVo hAppmentVo = new HAppmentVo();
            BeanUtils.copyProperties(item,hAppmentVo);
            return hAppmentVo;
        }).collect(Collectors.toList());
    }

    private List<HAd> getAd(AdType adType){
        return hAdDao.findByTypeAndStatus(adType.num,2);
    }

    private List<HAppment> getAppMent(AppMentType appMentType){
        return hAppmentDao.findByTypes(appMentType.num);
    }

    //点击广告增加次数
    public void addViewNum(String token){
        HUser hUser = tokenService.getUserInfoByToken(token);

        if (hUser.getFreeNum()+3 < hUser.getRemainNum()+1){
            return;
        }else {
            hUser.setRemainNum(hUser.getRemainNum()+1);
            hUser.setAllNum(hUser.getAllNum()+1);
            tokenService.updateUserInfo(hUser);
        }


    }

}
